package com.fbs.wowo.manage.mapper;

import com.fbs.wowo.manage.entity.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Set;

public interface RoleMapper {
    @Delete({
        "delete from role",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @InsertProvider(type=RoleSqlProvider.class, method="insertSelective")
    int insertSelective(Role record);

    @Select({
        "select",
        "id, name, description",
        "from role",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR)
    })
    Role selectByPrimaryKey(Integer id);

    @UpdateProvider(type=RoleSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Role record);

    /**
     * 按用户id查找角色
     * @param userId 用户 主键id
     * @return Role list
     */
    @Select({
            "select",
            "r.id, r.name, r.description",
            "from role r",
            "inner join user_role u",
            "on r.id = u.role_id",
            "where u.user_id = #{userId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR)
    })
    List<Role> findByUserId(String userId);

    /**
     * 按用户id查找角色字符串集合
     * @param userId
     * @return role string set
     */
    @Select({
            "select",
            "r.name",
            "from role r",
            "inner join user_role u",
            "on r.id = u.role_id",
            "where u.user_id = #{userId,jdbcType=VARCHAR}"
    })
    Set<String> findRoleStr(String userId);

    /**
     * 插入用户角色表
     * @param userId 用户id
     * @param roleId 角色id
     */
    @Insert({
            "insert into user_role (user_id, role_id)",
            "values (#{userId,jdbcType=VARCHAR}, #{roleId,jdbcType=INTEGER})"
    })
    void addRoleToUser(@Param("userId") String userId, @Param("roleId") Integer roleId);
}